﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Imports Tohto.Base.ExceptionPolicy.Config
Imports Tohto.Base.ExceptionPolicy.Interfaces

Namespace ExceptionHandler
    Public Class DefaultExceptionHandler
        Inherits ExceptionHandlerBase

        Public Sub New()
            MyBase.New()
        End Sub

        Private Sub LogEx(ByVal ex As Exception, ByVal strUserId As String, ByVal strErrorCode As String, ByVal LogConfigColletion As ExceptionConfig.LogHandlerConfigCollection)
            For Each logConfig As ExceptionConfig.LogHandlerConfig In LogConfigColletion
                Dim exLogHandler As IExceptionLogHandler = DirectCast(System.Activator.CreateInstance(Type.[GetType](logConfig.Type)), IExceptionLogHandler)
                exLogHandler.ExLog(ex, strErrorCode, m_ExHandlerConfig.Name, strUserId, logConfig)
            Next

            'foreach(IExceptionLogHandler exLogHandler in lstLogHandler)
            '			{
            '                exLogHandler.ExLog(ex, strErrorCode, m_ExHandlerConfig.Name, strUserId);
            '			}

        End Sub

        Public Overrides Function ProcessExeception(ByVal ex As Exception, ByVal strUserId As String, ByVal strErrorCode As String) As String
            LogEx(ex, strUserId, strErrorCode, MyBase.ExHanlderConfig.LogConfigColletion)

            Select Case m_ExHandlerConfig.ReturnMode
                Case ExceptionReturnMode.ErrorCode
                    If True Then
                        Return strErrorCode
                    End If
                Case ExceptionReturnMode.ErrorString
                    If True Then
                        Return ExPolicy.GetErrorInfoByCode(strErrorCode)
                    End If
                Case ExceptionReturnMode.ExceptionString
                    If True Then
                        Return ex.Message
                    End If
                Case ExceptionReturnMode.Exception
                    If True Then
                        If ex.GetBaseException() Is ex Then
                            Throw New Exception(String.Format("{0}LayerでExが発生しました:{1}，{2}", m_ExHandlerConfig.Name, strErrorCode, ex.Message), ex)
                        Else
                            Throw ex
                        End If
                    End If
                Case Else
                    If True Then
                        Return Nothing
                    End If
            End Select
            Return Nothing
        End Function
    End Class
End Namespace
